package com.credithc.signature.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.signature.common.mapper.TemplateMapper;
import com.credithc.signature.common.ro.core.CoreRequet;
import com.credithc.signature.common.ro.facade.RequestData;
import com.credithc.signature.common.service.ChannelService;
import com.credithc.signature.common.service.TemplateService;
import com.credithc.signature.manager.client.AuthClient;
import com.credithc.signature.manager.ro.SignatureRo;
import com.credithc.signature.manager.service.syncsign.SignSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述：同步签章调用<br/>
 * 作者：sunyang
 * 创建：2019/7/1 14:31
 * 版本：v1.0.0<br/>
 */
@RestController
@RequestMapping("/manager/signaturesync/v2")
@Slf4j
public class SignSyncController {

    @Autowired
    MessageFactory messageFactory;

    @Autowired
    private ChannelService channelService;

    @Autowired
    private TemplateService templateService;

    @Autowired
    private SignSyncService signSyncService;

    @Autowired
    AuthClient authClient;

    @Autowired
    private TemplateMapper templateMapper;


    /**
     * 模板预览功能
     *
     * @param signatureRo
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/signature")
    public String signature(SignatureRo signatureRo) throws Exception {
//        final String signatureSN = UUIDUtil.getUUID();
//        MDC.put(FacadeConstant.MDC_REQUEST_SN, signatureSN);
//
//        String templateNo = signatureRo.getTemplateNo();
//        Template template = templateService.selectByTemplateNo(templateNo);
//        Channel channel = channelService.selectById(template.getChannelId());
//        signatureRo.setEncryptKey(channel.getEncryptKey());
//        signatureRo.setChannelNo(channel.getChannelNo());
//
//        try {
//            // 参数校验
//            if (!channelService.existChannel(signatureRo.getEncryptKey(), signatureRo.getChannelNo())) {
//                return ResponseUtil.getResponseMessage(ResponseEnum.ILLEGAL_CHANNEL.getCode());
//            }
//
//            if (!templateService.existTemplate(signatureRo.getTemplateNo(), signatureRo.getNeedSign(), signatureRo.getChannelNo())) {
//                return ResponseUtil.getResponseMessage(ResponseEnum.ILLEGAL_TEMPLATE.getCode());
//            }
//            List<String> officialSealsCodes = signatureRo.getRequestData().getOfficialSealsCodes();
//            if (officialSealsCodes == null) {
//                if (!templateService.checkSeal(signatureRo.getTemplateNo(), signatureRo.getNeedSign(), signatureRo.getChannelNo())) {
//                    return ResponseUtil.getResponseMessage(ResponseEnum.ILLEGAL_OFFICIAL_SEAL.getCode());
//                }
//            } else {
//                if (!templateService.checkSeal(officialSealsCodes, signatureRo.getNeedSign(), signatureRo.getChannelNo(), signatureRo.getTemplateNo())) {
//                    return ResponseUtil.getResponseMessage(ResponseEnum.ILLEGAL_OFFICIAL_SEAL.getCode());
//                }
//            }
//            RestSignatureReq restSignatureReq = new RestSignatureReq();
//            BeanUtils.copyProperties(signatureRo, restSignatureReq);
//            return signSyncService.signature(restSignatureReq, signatureSN);
//        } finally {
//            try {
//                MDC.remove(FacadeConstant.MDC_REQUEST_SN);
//            } catch (Exception e) {
//                // 不做处理
//            }
//        }
        return "该功能正在维护......";
    }

    @ResponseBody
    @PostMapping("/signature/repeat")
    public Message signatureAgain(@RequestBody SignatureRo signatureRo) throws Exception {
//        String templateNo = signatureRo.getTemplateNo();
//        CoreRequet coreRequet = buildCoreRequet(signatureRo);
//        RabbitSender rabbitSender = (RabbitSender) BeanTool.getBean("managerRabbitSender");
//        //判断是否为vip通道
//        Template template = templateMapper.selectByTemplateNo(templateNo);
//        rabbitSender.sendSync(coreRequet, template.getVip());
//        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, "放入队列成功");
        return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "该功能正在维护");
    }

    /**
     * 封装请求参数
     *
     * @param signatureRo
     * @return
     */
    private CoreRequet buildCoreRequet(SignatureRo signatureRo) {
        JSONObject object = new JSONObject();
        String requestSn = signatureRo.getRequestSn();
        String templateNo = signatureRo.getTemplateNo();
        String channelNo = signatureRo.getChannelNo();
        Boolean needSign = signatureRo.getNeedSign();
        String callBackUrl = signatureRo.getCallBackUrl();
        RequestData requestData = signatureRo.getRequestData();
        boolean isNewSignatrue = false;
        if (requestData.getCertInfos() != null || requestData.getListData() != null || requestData.getOtherData() != null) {
            isNewSignatrue = true;
            object.put("certInfos", requestData.getCertInfos());
            object.put("listData", requestData.getListData());
            object.put("otherData", requestData.getOtherData());
            object.put("officialSealsCodes", requestData.getOfficialSealsCodes());
        } else {
            object.put("certApplys", requestData.getCertApplys());
            object.put("listElements", requestData.getListElements());
            object.put("singnalElement", requestData.getSingnalElement());
            object.put("officialSealsCodes", requestData.getOfficialSealsCodes());
        }

        CoreRequet coreRequet = new CoreRequet(requestSn, templateNo, channelNo, needSign, callBackUrl, object.toJSONString(), isNewSignatrue);
        return coreRequet;
    }

}
